package us.icitap.commandcenter.ui.dispatcher;

import java.util.Date;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.Result;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallReason;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusEnum;
import us.icitap.commandcenter.business.ejb.persistence.CallStatusHistory;
import us.icitap.commandcenter.business.ejb.persistence.PoliceAction;
import us.icitap.commandcenter.listeners.ManageCallListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustComboBox;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.ui.custom.CustTextField;
import us.icitap.commandcenter.utils.HandleNotification;
import us.icitap.commandcenter.utils.ProjectLists;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.ThemeResource;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.CloseEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TwinColSelect;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class CloseVerificationWindow extends Window implements ClickListener {

	private Call call;
	private ManageCallListener manageCallListener;
	
	BeanFieldGroup<Call> binder = new BeanFieldGroup<Call>(Call.class);
	
	private final CustTextField patrolsTextField = new CustTextField("Patrulla qe caktohet per verifikim");
	
	private final CustTextArea givenOrdersTextField = new CustTextArea("Urdherat qe jepen");
	private final CustTextArea verificationNotesTextArea = new CustTextArea("Shenime mbi verifikimin e telefonates");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");
	
	private final CheckBox falseCallCheckBox = new CheckBox("Telefonate e rreme");
	
	private final CustComboBox verifiedCallReasonComboBox = new CustComboBox("Rezultati i verifikimit");
	
	private TwinColSelect policeActionTwinColSelect = new TwinColSelect();
	
	String previousVerificationNotes;
	String previousPatrols;
	String previousGivenOrders;
	
	final Embedded embPreviousPatrols = new Embedded(null,new ThemeResource("img/icons/blue/Info.png"));
	final Embedded embPreviousVerNotes = new Embedded(null,new ThemeResource("img/icons/blue/Info.png"));
	final Embedded embPrevoiusGivenOrders = new Embedded(null,new ThemeResource("img/icons/blue/Info.png"));
	
	public CloseVerificationWindow(ManageCallListener manageCallListener, Call call) {
	
		this.manageCallListener = manageCallListener;
		this.setCall(call);
		
		
		this.setCaption("Te dhena shtese per telefonaten");
		this.setModal(true);
		this.center();
		this.setResizable(false);
		
		
		binder.bind(patrolsTextField, "patrols");
		binder.bind(this.givenOrdersTextField, "givenOrders");
		binder.bind(falseCallCheckBox, "falseCall");
		binder.bind(verificationNotesTextArea, "verificationNotes");
		binder.bind(verifiedCallReasonComboBox, "verifiedCallReason");
		binder.bind(policeActionTwinColSelect, "policeActionList");
		
		
		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		vl.setSpacing(true);
		
		
		HorizontalLayout hl1 = new HorizontalLayout();
		hl1.addComponent(patrolsTextField);
		hl1.addComponent(this.embPreviousPatrols);
		vl.addComponent(hl1);
		
		HorizontalLayout hl2 = new HorizontalLayout();
		hl2.addComponent(givenOrdersTextField);
		this.givenOrdersTextField.setWidth("450px");
		hl2.addComponent(this.embPrevoiusGivenOrders);
		vl.addComponent(hl2);
		
		HorizontalLayout hl3 = new HorizontalLayout();
		hl3.addComponent(verificationNotesTextArea);
		this.verificationNotesTextArea.setWidth("450px");
		hl3.addComponent(this.embPreviousVerNotes);
		vl.addComponent(hl3);
		
		vl.addComponent(falseCallCheckBox);
		
		
		vl.addComponent(verifiedCallReasonComboBox);
		
		List<CallReason> callReasonList =  ProjectLists.getCallReasonList();
		
		verifiedCallReasonComboBox.setContainerDataSource(new BeanItemContainer<CallReason>(CallReason.class, callReasonList));
		verifiedCallReasonComboBox.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		verifiedCallReasonComboBox.setItemCaptionPropertyId("tag");
		
		
		
		policeActionTwinColSelect.setWidth("600px");
		policeActionTwinColSelect.setLeftColumnCaption("Lista e masave te mundshme");
		policeActionTwinColSelect.setRightColumnCaption("Lista e masave te marra");
		policeActionTwinColSelect.setImmediate(true);
		policeActionTwinColSelect.setNullSelectionAllowed(true);
		policeActionTwinColSelect.setContainerDataSource(new BeanItemContainer<PoliceAction>(PoliceAction.class, ProjectLists.getPoliceActionList()));
		policeActionTwinColSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT_DEFAULTS_ID);
		policeActionTwinColSelect.setItemCaptionPropertyId("tag");
		
		vl.addComponent(policeActionTwinColSelect);
		
		
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("600px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		
		this.setContent(vl);
		
		this.saveButton.addClickListener(this);
		
		this.addCloseListener(new CloseListener() {
			
			@Override
			public void windowClose(CloseEvent e) {
				
				CloseVerificationWindow.this.call.setPatrols(previousPatrols);
				CloseVerificationWindow.this.call.setGivenOrders(previousGivenOrders);
				CloseVerificationWindow.this.call.setVerificationNotes(previousVerificationNotes);
			}
		});
	
	}
	
	
	public void setCall(Call call){
		this.call = call;
		
		if(call.getPatrols() != null){
			this.previousPatrols = new String(call.getPatrols());
			call.setPatrols(null);
			this.embPreviousPatrols.setDescription("<h3>Patrullat e derguara</h3>" + this.previousPatrols);
		}
		else{
			this.previousPatrols = "";
			this.embPreviousPatrols.setDescription("<h3>Akoma nuk eshte derguar asnje patrulle</h3>");
		}
			
		
		if(call.getGivenOrders() != null){
			this.previousGivenOrders = new String(call.getGivenOrders());
			call.setGivenOrders(null);
			this.embPrevoiusGivenOrders.setDescription("<h3>Urdherat e dhene</h3>" + this.previousGivenOrders);
		}
		else{
			this.previousGivenOrders = "";
			this.embPrevoiusGivenOrders.setDescription("<h3>Akoma nuk eshte dhene asnje urdher</h3>");
		}
			
		
		if(call.getVerificationNotes() != null){
			this.previousVerificationNotes = new String(call.getVerificationNotes());
			call.setVerificationNotes(null);
			this.embPreviousVerNotes.setDescription("<h3>Shenimet e meparshme</h3>" + this.previousVerificationNotes);
		}
		else{
			this.previousVerificationNotes = "";
			this.embPreviousVerNotes.setDescription("<h3>Akoma nuk eshte rregjistruar asnje shenim</h3>");
		}
		binder.setItemDataSource(this.call);
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			binder.commit();

			if(call.getPatrols() != null ){
				if(!this.previousPatrols.equals(""))
					call.setPatrols(this.previousPatrols + ", " + call.getPatrols());
			}
			else{
				call.setPatrols(this.previousPatrols);
			}
			
			if(call.getGivenOrders() != null){
				if(!this.previousGivenOrders.equals(""))
					call.setGivenOrders(this.previousGivenOrders + ", " + call.getGivenOrders());
			}
				
			else{
				call.setGivenOrders(this.previousGivenOrders);
			}
			
			if(call.getVerificationNotes() != null){
				if(!this.previousVerificationNotes.equals(""))
					call.setVerificationNotes(this.previousVerificationNotes + "<br/>" + call.getVerificationNotes());
			}
				
			else{
				call.setVerificationNotes(this.previousVerificationNotes);
			}
			
			
			call.setActualStatus(CallStatusEnum.V);
			call.setVerificationDate(new Date());
			
			
			this.manageCallListener.closeCallVerification(call);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
